package com.example.skytakeoutserver.config


import ch.qos.logback.classic.spi.ConfiguratorRank
import com.example.serverbase.ext.Log
import org.springframework.context.annotation.Bean
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory
import org.springframework.data.redis.core.ReactiveRedisTemplate
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
import org.springframework.data.redis.serializer.RedisSerializationContext
import org.springframework.data.redis.serializer.StringRedisSerializer

/**
 * 配置Redis
 */
@ConfiguratorRank
class RedisConfig {
    /**
     * 提供redis
     */
    @Bean
    fun reactiveRedisTemplate(
        factory: ReactiveRedisConnectionFactory
    ): ReactiveRedisTemplate<String, Any> {
        val keySerializer = StringRedisSerializer()
        // redisTemplate序列化java8 时间类Instant报错Java 8 date/time type `java.time.Instant` not supported by default
        //val objectMapper = ObjectMapper()
        //objectMapper.registerModule(JavaTimeModule())

        val valuerSerializer = Jackson2JsonRedisSerializer(Any::class.java)

        val builder = RedisSerializationContext
            .newSerializationContext<String, Any>(keySerializer)
        val context = builder.value(valuerSerializer).build()

        Log.info("+++++++++++++++++++RedisConfig初始化+++++++++++++++++++")

        return ReactiveRedisTemplate(factory, context)
    }
}